package com.qyl.honghengshop.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.qyl.honghengshop.dao.AdminUserDao;
import com.qyl.honghengshop.dao.BaseDao;
import com.qyl.honghengshop.dto.User;
import com.qyl.honghengshop.mapper.OrderMapper;
import com.qyl.honghengshop.mapper.UserMapper;
import com.qyl.honghengshop.utils.PageBean;


public class AdminUserDaoImpl extends BaseDao implements AdminUserDao {

	@Override
	public void getUserList(PageBean pageBean,User user) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		List<User> userList = userMapper.getUserList(pageBean,user);
		
		pageBean.setList(userList);
		
		//关闭sqlSession
		this.closeSqlSession();

	}
	
	@Override
	public int getRecordCountByUser(User user) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		int count = userMapper.getRecordCountByUser(user);
		
		//关闭sqlSession
		this.closeSqlSession();
	
		return count;
	}

	@Override
	public int addUser(User user) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		int count = userMapper.addUser(user);
		
		sqlSession.commit();
		//关闭sqlSession
		this.closeSqlSession();
		
		return count;
	}

/*
 * 删除用户
 * 	(non-Javadoc)
 * @see com.qyl.honghengshop.dao.AdminUserDao#delUser(java.lang.Integer)
 */
	@Override
	public void delUser(Integer id) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		try {
			//把与user表关联的外键置为null
			OrderMapper orderMapper = sqlSession.getMapper(OrderMapper.class);
			
			orderMapper.setUserIdtoNull(id);
			
			UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
			
			userMapper.delUser(id);
			
			sqlSession.commit();			
			
		} catch (Exception e) {
			
			sqlSession.rollback();
			e.printStackTrace();
		}		
		//关闭sqlSession
		this.closeSqlSession();		
	}

	@Override
	public User getUserById(Integer id) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		User updateUser = userMapper.getUserById(id);
		
		//关闭sqlSession
		this.closeSqlSession();		
		
		return updateUser;
	}

	@Override
	public int update(User user) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		int count = userMapper.update(user);
		
		sqlSession.commit();
		//关闭sqlSession
		this.closeSqlSession();		
		
		return count;
	}

	@Override
	public int delBatchUser(int[] ids) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		int count = userMapper.delBatchUser(ids);
		
		sqlSession.commit();
		//关闭sqlSession
		this.closeSqlSession();		
		
		return count;
		
	}

	@Override
	public void updateUserHead(User user) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		userMapper.updateUserHead(user);
		
		sqlSession.commit();
		//关闭sqlSession
		this.closeSqlSession();		
		
		
	}

	@Override
	public void register(User user) {
		
		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		userMapper.register(user);
		
		sqlSession.commit();
		//关闭sqlSession
		this.closeSqlSession();		
	}

	@Override
	public void activeUser(String activeCode) {

		//获得sqlSession
		SqlSession sqlSession = this.getSqlSession();
		
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		
		userMapper.activeUser(activeCode);
		
		sqlSession.commit();
		//关闭sqlSession
		this.closeSqlSession();		
		
	}

}
